import { Injectable, Inject } from '@angular/core';
import {
  CanActivate,
  RouterStateSnapshot,
  Router,
  UrlTree,
  ActivatedRouteSnapshot
} from '@angular/router';

import { AuthService } from '../services/auth.service';
import { Observable } from 'rxjs';
import { LuckyTableService } from '../services/lucky-table.service';

@Injectable({
  providedIn: 'root'
})
export class FrontGuard implements CanActivate {
  constructor(
    private luckyTableService: LuckyTableService,
    private router: Router
  ) {}
  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ):
    | Observable<boolean | UrlTree>
    | Promise<boolean | UrlTree>
    | boolean
    | UrlTree {
    const id = next.params.id;
    if (id.length !== 24) {
      this.router.navigate(['/user']);
      return false;
    }
    return this.luckyTableService.checkTable(id);
  }
}
